<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="workstationtoolList" @selection-change="handleSelectionChange">
      <el-table-column label="类型名称" align="center" prop="toolTypeName" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="操作" align="center" v-if="optType !='view'" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:md:workstation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:md:workstation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 添加或修改工装夹具资源对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="Workstationtoolform" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="工装夹具类型" prop="toolTypeId">
           <el-select v-model="form.toolTypeId" placeholder="请选择类型">
                <el-option
                  v-for="dict in toolTypeOptions"
                  :key="dict.toolTypeId"
                  :label="dict.toolTypeName"
                  :value="dict.toolTypeId"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input-number :min="1" v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
    </template>
    </el-dialog>
  </div>
</template>

<script setup class="Workstationtool">
import { listWorkstationtool, getWorkstationtool, delWorkstationtool, addWorkstationtool, updateWorkstationtool } from "@/api/mes/md/workstationtool";
import { listAllTooltype } from "@/api/mes/tm/tooltype"

const { proxy } = getCurrentInstance();

const loading = ref(true);// 遮罩层
const ids = ref([]); // 选中数组
const showSearch = ref(true);// 显示搜索条件
const single = ref(true);// 非单个禁用
const multiple = ref(true);// 非多个禁用
const total = ref(0);// 总条数
const title = ref(""); // 弹出层标题
const open = ref(false);// 是否显示弹出层

// 工装夹具资源表格数据
const workstationtoolList = ref([]); 
const toolTypeOptions = ref([]); 

const data = reactive({
  form: {},// 表单参数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workstationId: this.workstationId,
        toolTypeId: null,
        toolTypeCode: null,
        toolTypeName: null,
        quantity: null,
      },
      // 表单校验
      rules: {
        workstationId: [
          { required: true, message: "工作站ID不能为空", trigger: "blur" }
        ],
        toolTypeId: [
          { required: true, message: "工装夹具类型ID不能为空", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
      }
});

const { queryParams, form, rules } = toRefs(data);//对象解构


/** 查询工装夹具资源列表 */
function getList() {
  loading.value = true;
  listWorkstationtool(queryParams.value).then(response => {
    workstationtoolList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function getTypeList(){
  listAllTooltype().then(response =>{
    toolTypeOptions.value = response.data;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 表单重置
function reset() {
  form.value = {
    recordId: null,
    workstationId: this.workstationId,
    toolTypeId: null,
    toolTypeCode: null,
    toolTypeName: null,
    quantity: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("Workstationtoolform");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.recordId)
  single.value = selection.length!==1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加工装夹具资源";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const recordId = row.recordId || ids.value
  getWorkstationtool(recordId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改工装夹具资源";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["Workstationtoolform"].validate(valid => {
    if (valid) {
      if (form.value.recordId != null) {
        updateWorkstationtool(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWorkstationtool(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const recordIds = row.recordId || ids.value;
  proxy.$modal.confirm('是否确认删除工装夹具资源？').then(function() {
    return delWorkstationtool(recordIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
getTypeList();




  // props: {
  //   workstationId: undefined,
  //   optType: undefined,
  // },



</script>
